package cn.com.ddapp.ddm.ui;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.net.Uri;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import cn.com.ddapp.ddm.AppConfig;
import cn.com.ddapp.ddm.R;
import cn.com.ddapp.ddm.adapter.NearstoreAdpter;
import cn.com.ddapp.ddm.api.OKUtilAPI;
import cn.com.ddapp.ddm.base.BaseActivity;
import cn.com.ddapp.ddm.base.BaseApplication;
import cn.com.ddapp.ddm.bean.NearStoreBean;
import cn.com.ddapp.ddm.inter.OkUtilCallback;
import cn.com.ddapp.ddm.ui.empty.EmptyLayout;
import cn.com.ddapp.ddm.widget.pulltorefresh.PullToRefreshLayout;
import cn.com.ddapp.ddm.widget.pulltorefresh.pullableview.PullableListView;
import okhttp3.Call;


/**
 * Created by Administrator on 2016/7/29 0029.
 */
public class NearStoreActivity extends BaseActivity {
	@BindView(R.id.nearshop_refresh)
	PullToRefreshLayout nearshop_refresh;
	@BindView(R.id.nearshop_list)
	PullableListView nearshop_list;
	@BindView(R.id.empty)
	EmptyLayout emptyLayout;
	@BindView(R.id.img_error_layout)
	ImageView img_error_layout;
	private NearstoreAdpter nAdapter;
	private List<NearStoreBean.ShopBean> nList;
	private int st=0;
    Dialog dialog;
	public static void startIntent(Context context){
		Intent intent=new Intent(context,NearStoreActivity.class);
		context.startActivity(intent);
	}

	@Override
	public void initView() {

		setTitle("配送点");
		setTitleLeftImg(R.mipmap.back_icon);
//		TextUtil.setFlags(welcome);
			requestData(0);
		nearshop_refresh.setOnRefreshListener(new PullToRefreshLayout.OnRefreshListener() {
			@Override
			public void onRefresh(PullToRefreshLayout pullToRefreshLayout) {
				st=0;
				requestData(0);
				nearshop_refresh.refreshFinish(PullToRefreshLayout.SUCCEED);
			}

			@Override
			public void onLoadMore(PullToRefreshLayout pullToRefreshLayout) {
				st=st+10;
				requestLoadData(st);
			}
		});

	}



	private void requestData(int i) {
		emptyLayout.setErrorType(EmptyLayout.NETWORK_LOADING);
		final OkUtilCallback okUtilCallback = new OkUtilCallback(emptyLayout) {
			@Override
			public void onMyResponse(String response, int id) {
				NearStoreBean bean = gson.fromJson(response, NearStoreBean.class);
				if(!bean.getResult().equals("OK")){
					emptyLayout.setErrorType(EmptyLayout.NODATA);
				}else if(bean.getResult().equals("OK") && bean.getShopDisList().size()==0){
					emptyLayout.setErrorType(EmptyLayout.NODATA);
				}else{
					nList=bean.getShopDisList();
					nAdapter=new NearstoreAdpter(NearStoreActivity.this,nList);
					nearshop_list.setAdapter(nAdapter);
                    dialog=new Dialog(mContext);
                    nearshop_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                        @Override
                        public void onItemClick(AdapterView<?> adapterView, View view, final int i, long l) {
                            dialog.setContentView(R.layout.nearshop_dialog);
                            TextView custom_tell= (TextView) dialog.findViewById(R.id.custom_tell);
                            TextView details_map= (TextView) dialog.findViewById(R.id.details_map);
                            TextView details_dismiss= (TextView) dialog.findViewById(R.id.details_dismiss);
                            custom_tell.setText("客服热线:"+nList.get(i).getTel());
                            custom_tell.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View view) {
                                    Intent  gg = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + nList.get(i).getTel()));//跳转到拨号界面，同时传递电话号码
                                    startActivity(gg);
                                    dialog.dismiss();
                                }
                            });
                            details_map.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View view) {
                                    Intent intent=new Intent(NearStoreActivity.this,NearStoreDetailsActivity.class);
                                    intent.putExtra("lat",nList.get(i).getLatitude());
                                    intent.putExtra("lng",nList.get(i).getLongitude());
                                    intent.putExtra("whName",nList.get(i).getName());
                                    startActivity(intent);
                                    dialog.dismiss();

                                }
                            });
                            details_dismiss.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View view) {
                                    dialog.dismiss();
                                }
                            });
                            Window window=dialog.getWindow();
                            window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
                            dialog.setCanceledOnTouchOutside(true);
                            WindowManager.LayoutParams params=dialog.getWindow().getAttributes();
                            params.gravity=Gravity.BOTTOM;
                            dialog.getWindow().setAttributes(params);
                            try{
                                Context context = dialog.getContext();
                                int divierId = context.getResources().getIdentifier("android:id/titleDivider", null, null);
                                View divider = dialog.findViewById(divierId);
                                divider.setBackgroundColor(Color.TRANSPARENT);
                            }catch(Exception e){
                                //去除Holo主题的蓝色线条
                                e.printStackTrace();
                            }
                            dialog.show();

                        }
                    });
				}
			}

			@Override
			public void onError(Call call, Exception e, int id) {
				super.onError(call, e, id);
                emptyLayout.setErrorType(EmptyLayout.NETWORK_ERROR);
				BaseApplication.getIntstance().showToast(R.string.network_error, Toast.LENGTH_SHORT, 0, Gravity.CENTER);
			}
		};
		String params="longitude=" +AppConfig.LNG+"&latitude="+ AppConfig.LAT+"&st="+st;
		OKUtilAPI.post(OKUtilAPI.nearstore, params, okUtilCallback);
	}
	private void requestLoadData(final int a) {
//		final OkUtilCallback okUtilCallback =new OkUtilCallback() {
//			@Override
//			public void onMyResponse(String response, int id) {
//				NearStoreBean bean = gson.fromJson(response, NearStoreBean.class);
//				List<NearStoreBean.ShopBean> ll=bean.getShopDisList();
//				Log.i("yu","--size-"+ll.size());
//				if(ll.size()>0){
//					st=a;
//					nAdapter.addView(ll);
//					nAdapter.notifyDataSetChanged();
//					nearshop_refresh.refreshFinish(PullToRefreshLayout.SUCCEED);
//				}else{
//                    nearshop_refresh.loadmoreFinish(PullToRefreshLayout.NODATA);
//				}
//			}
//		};
//
//		String params="longitude=" +AppConfig.LNG+"&latitude="+AppConfig.LAT+"&st="+st;
//		OKUtilAPI.post(OKUtilAPI.nearstore, params, okUtilCallback);
        nearshop_refresh.loadmoreFinish(PullToRefreshLayout.NODATA);
	}
	@Override
	public void initData() {
//		nAdapter=new NearstoreAdpter(this,nList);
//		nearshop_list.setAdapter(nAdapter);
	}

	@Override
	public int getContentViewId() {
		return R.layout.activity_nearshop;
	}

	@Override
	protected String getFragmentTag() {
		return null;
	}

	private void reequestData() {

	}


	@OnClick({R.id.img_error_layout})
	public void onClick(View v) {
		switch (v.getId()) {
			case R.id.img_error_layout:
				requestData(0);
				break;


		}
	}

}
